<?php

namespace app\index\controller;

use app\admin\model\Coupon;
use app\common\controller\Frontend;
use app\common\library\Yunding;
use fast\Http;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function testYundingElemetor()
    {
        $homeId = '62a2c3a6c0115a56d4c9141a';
        $uuid = '3a225058750d6bf8610902003d249d4c';
        $yunding = new Yunding();
        $elem = $yunding->isElemeterSwitchOn($homeId, $uuid);
//        $elem = $yunding->elemeterSwitchOff($homeId, $uuid);

        var_dump($elem);
    }


    // 第三步， 通过session, 得到固定的open_shop_uuid
    // 返回 array(3) { ["code"]=> int(200) ["msg"]=> string(7) "success" ["data"]=> array(1) { [0]=> array(5) { ["open_shop_uuid"]=> string(32) "32effb74875ce51cb703c27601bb68c8" ["shopname"]=> string(27) "五十自助舞蹈练习室" ["branchname"]=> string(0) "" ["shopaddress"]=> string(18) "大业路16号1807" ["cityname"]=> string(6) "成都" } } }
    public function getShopUUID()
    {
        $appKey = 'c6200eb0f39b2824';
        $appSecret = '133a9941129b0e6eddf08a999bb1679cb83bbfd7';

        $timestamp = date('Y-m-d H:i:s');

        $format = 'json';

        $v = 1;

        $session = '7eea8c010fc1be53972cd09d0b29378f68d04d40';

        $data = [
            'app_key' => $appKey,
            'sign_method' => 'MD5',
            'timestamp' => $timestamp,
            'v' => $v,
            'format' => $format,
            'session' => $session,
            'bid' => '136693499ed0d4d82a124764cd8eac3b',
        ];

        ksort($data);

        $sign = $this->cal_sign($appSecret, $data);//获取签名

        $data['sign'] = $sign;

        $postdata = http_build_query($data);

        $url = 'https://openapi.dianping.com/router/oauth/session/scope?' . $postdata;

        $tmpInfo=$this->curl_get($url);

        var_dump($tmpInfo);

    }


    // 第二步， 通过auth_code, 得到session

    public function getSession()
    {
        $appKey = '4b00607f2b56265c';
        $appSecret = 'f75974fe167b61f9bf30dadeb2b1343cc0aab940';
        $authCode = '9e7e61f462137ab9d727463364395d2bf25f1623';

        $url = 'https://openapi.dianping.com/router/oauth/token';
        $data = [
            'app_key' => $appKey,
            'app_secret' => $appSecret,
            'auth_code' => $authCode,
            'grant_type' => 'authorization_code',
            'redirect_url' => 'https://dancegaden.firstmove.club/index/index/meituanredirect',
        ];
        $postdata = http_build_query($data);
        $kk = $this->curl_post($url, $postdata);

        //array(9) { ["code"]=> int(200) ["msg"]=> string(7) "success" ["access_token"]=> string(40) "ff9cc61aa818f6ddc317f80173fae70cada31d33" ["expires_in"]=> int(2591999) ["remain_refresh_count"]=> int(12) ["tokenType"]=> string(6) "bearer" ["scope"]=> string(7) "tuangou" ["bid"]=> string(32) "930e3b7c06180486e18f83235598b704" ["refresh_token"]=> string(40) "25161648dc9e0792bca39ef316a2607ce6e39b77" }

        var_dump($kk);
    }

    //  第一步， 通过下面的请求得到auth_code
    //  https://e.dianping.com/dz-open/merchant/auth?app_key=4b00607f2b56265c&redirect_url=https://dancegaden.firstmove.club/index/index/meituanredirect&state=teststate&scope=[%22tuangou%22,%22ugc%22]

    public function meituanredirect()
    {
        $authCode = $this->request->get('auth_code');
        var_dump($authCode);
    }

    protected function cal_sign($app_secret, $req_param){
        // 排序所有请求参数
        ksort($req_param);
        $src_value = "";
        // 按照key1value1key2value2...keynvaluen拼接
        foreach ($req_param as $key=>$value){
            $src_value.=($key.$value);
        }
        //计算md5
        return md5($app_secret.$src_value.$app_secret);
    }

    private function curl_post($url,$postdata){
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Expect:'
        )); // 解决数据包大不能提交
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        $tmpInfo=json_decode($tmpInfo,true);
        return $tmpInfo;
    }

    //get请求
    private function curl_get($url) {
        //初使化curl
        $curl = curl_init();
        //请求的url，由形参传入
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Expect:'
        )); // 解决数据包大不能提交
        //将得到的数据返回
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //不处理头信息
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //连接超过10秒超时
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        //执行curl
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);
        }
        //关闭资源
        curl_close($curl);
        //返回内容
        $tmpInfo=json_decode($output,true);
        return $tmpInfo;
    }

    public function kkk()
    {
        $cc = Coupon::where('id', 4)->find();
        var_dump($cc);
    }

}
